import axios from '@peng_kai/kit/libs/axios';
import {
  checkCode,
  filterEmptyValue,
  formatPaging,
  popupMessage,
  returnResultType,
  unitizeAxiosError,
} from '@peng_kai/kit/request/interceptors';
import { antdMessage } from '~/components/antd-assist';

export const server = axios.create({
  baseURL: import.meta.env.VITE_URL_PREFIX_blog,
  timeout: 10 * 1000,
  // 自定义属性
  resultType: 'data',
  joinTime: true,
  errorMessage: true,
  successMessage: false,
  checkCode: true,
});

/* #B 请求拦截器（后 use 先执行） */
// 过滤掉查询参数中的空值
server.interceptors.request.use(...filterEmptyValue());

// 处理全局请求 Header
server.interceptors.request.use((req) => {
  // 删除多余的
  // ['Service-Name'].forEach((k) => req.headers.delete(k));
  // req.headers.set('uid', '1');
  req.headers.set('version', window.__APP_VERSION__ || '1.0.0');
  req.headers.set('lang', document.documentElement.lang);
  // req.headers.set('token', 'MTcyNDc1MTM5OXxOd3dBTkZwUU16UTJWVEpUUlZoTFJsVkpOMHRQUkRWRFNUTXpSalZYVGpKUVRWSkxVRTVQU1ZNM1RUTklSMFpKVlU1WFdVWk1Xa0U9fNvVQ5ZPOEJ1BHfhI__LWqJJf6pvgn8okv3miHlRS1zE');

  return req;
});
/* #E */

/* #B 响应拦截器（先 use 先执行） */
server.interceptors.response.use(...formatPaging());
server.interceptors.response.use(...unitizeAxiosError());
server.interceptors.response.use(...checkCode());
server.interceptors.response.use(...popupMessage((type, msg) => antdMessage.open({ key: msg, type, content: msg })));
server.interceptors.response.use(...returnResultType());
/* #E */
